<?php

namespace App\Http\Controllers;

use App\Models\ActivityProjectVideo;
use App\Models\Maker;
use App\Models\Project;
use App\Models\Topic;
use App\Models\TopicPhoto;
use App\Models\TopicPost;
use App\Models\TopicPostPhoto;
use App\Models\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User as AUser;
use App\Http\Requests\TopicPhotoRequest;
class TopicPhotoController extends CommonController
{
    /**
     * 获取单条专题图片管理界面
     */
    public function getIndex(Request $request)
    {
        $data = $request->input();
        $topics = TopicPost::where('topic_id', $data['id'])->get()->toArray();
        $topicIds = array_column($topics,'id');
        $photos = TopicPostPhoto::whereIn('topic_post_id', $topicIds)->orderBy('topic_post_id','desc')->paginate(10);

        return view('topicphoto.index')->with("lists", $photos)->with("actions", $this->actions)->with("topic_id", $data['id']);
    }

    /**
     * 获取增加图片的界面
     *
     */
    public function getAddPhoto(Request $request)
    {
        $data = $request->input();
        $topic_id = $data['topic_id'];
        $topics = TopicPost::where('topic_id', $data['topic_id'])->get();

        return view('topicphoto.create')->with('topic_id', $topic_id)->with('topics', $topics);
    }

    /**
     * 执行图片地址添加
     */
    public function postAddPhoto(TopicPhotoRequest $request)
    {
        $data = $request->input();

//        dd($data);
        $topic_id = $data['topic_id'];
        unset($data['_token'], $data['formType'], $data['topic_id']);
        TopicPostPhoto::create($data);
//        $max = count($data)/3+1;
//        $m=0;
//        for($i=1; $i<$max; $i++){
//            $insert_data = array_values(array_slice($data, ($i-1)*3, 3));
//            $insert_data[0]!='' && TopicPhoto::create(['topic_id'=>$topic_id, 'photo'=>$insert_data[0], 'description'=>$insert_data[1], 'sort'=>$insert_data[2]]);
//            $insert_data[0]=='' && $m++;
//        }
//
//        if($m) {
//            return AjaxCallbackMessage('图片新增成功,其中有'.$m.'张没有上传,原因是你没有传图片', true);
//        }

        return AjaxCallbackMessage('图片新增成功', true, url('topicphoto/index?id=' . $topic_id));
    }

    /**
     * 删除一张图片
     */
    public function postDelPhoto(Request $request)
    {
        $data = $request->input();
        TopicPostPhoto::where('id', $data['pid'])->delete();

        return AjaxCallbackMessage('图片删除成功', true, url('topicphoto/index?id=' . $data['topic_id']));
    }

    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = TopicPostPhoto::where('id', $data['id'])->first();
        $topic_id = $data['topic_id'];
        $topics = TopicPost::where('topic_id', $data['topic_id'])->get();

        return view('topicphoto.edit')->with('list', $list)->with('topic_id', $topic_id)->with('topics', $topics);
    }


    public function postEdit(TopicPhotoRequest $request)
    {
        $data = $request->input();
        $topic_id = $data['topic_id'];
        unset($data['_token'], $data['formType'], $data['topic_id']);
        TopicPostPhoto::where('id', $data['id'])->update($data);

        return AjaxCallbackMessage('图片编辑成功', true, url('topicphoto/index?id=' . $topic_id));

    }
}
